import * as $util from '/lib/hc-util.js'

export const $fu = {
    viewId: function(intId) {
        return 'nid' + intId
    },

    modelId: function(domId) {
        return parseInt(domId.substr(3))
    },

    lineLabelId: function(lineModel) {
        return 'nid' + lineModel.sourceId +
            '-nid' + lineModel.targetId + '-lid' + lineModel.id;
    },

    conLabelId: function(connection) {
        return connection.sourceId + '-' + connection.targetId + '-' + connection.id
    },

    isFlowNode: function(val) {
        let itemType = typeof(val) == 'object' ? val.itemType : val;

        let index = ['task', 'start', 'end', 'switch', 'merge'].findIndex(it => it == itemType)

        return index >= 0
    },

    lineModelId: function(val) {
        return parseInt(val.substr('lid'.length, val.length - 'lid'.length))
    },

    nopresent: {},

    getPosition: function(domIdOrDom) {
        let f = $util.position(domIdOrDom);
        return {
            left:f.left(),
            top:f.top(),
        }
        // return getElePos(typeof(domIdOrDom) == 'object' ? domIdOrDom : document.getElementById(
        //     domIdOrDom));

        // function getElePos(ele) {
        //     let style = ele.style

        //     let left = style.left
        //     left = left.substr(0, left.length - 2) //remove 'px'
        //     let top = style.top
        //     top = top.substr(0, top.length - 2) //remove 'px'

        //     return {
        //         left: parseInt(left),
        //         top: parseInt(top)
        //     }
        // };
    },

};

export const item$type$map = {
    'start': {
        name: '开始',
        css: 'flownode start',
    },
    'task': {
        name: '任务',
        css: 'flownode task',
    },
    'switch': {
        name: '分支',
        css: 'flownode switch',
    },
    'merge': {
        name: '合并',
        css: 'flownode merge',
    },
    'end': {
        name: '结束',
        css: 'flownode end',
    },
    'branch': {
        name: '分支',
    },
    'route': {
        name: '连接'
    }
};

export const hc$flowitem$type$map = {
    'start': '开始',
    'task': '任务',
    'switch': '分支',
    'merge': '合并',
    'end': '结束',
    'branch': '条件',
    'route': '连接',
};

export const hc$flowitem$type$list = $util.prop2list(hc$flowitem$type$map);
